package com.baihan.angus.app.game.busshow.ui.home;

import android.graphics.Typeface;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;

import com.baihan.angus.app.game.basic.base.BaseFragment;
import com.baihan.angus.app.game.busshow.BR;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.databinding.FragmentHomeBinding;
import com.baihan.angus.app.game.busshow.ui.home.follow.FollowFragment;
import com.baihan.angus.app.game.busshow.ui.home.recommend.RecommendFragment;
import com.google.android.material.tabs.TabLayout;
import com.gyf.immersionbar.ImmersionBar;

/**
 * 首页
 */
public class HomeFragment extends BaseFragment<FragmentHomeBinding, HomeVM> {

    private FollowFragment mFollowFragment;
    private RecommendFragment mRecommendFragment;
//    private NearbyFragment mNearbyFragment;

    public HomeFragment() {
        // Required empty public constructor
    }

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getViewModelId() {
        return BR.homeVM;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {

        LinearLayout.LayoutParams rlTitle = (LinearLayout.LayoutParams) mVDBinding.title.getLayoutParams();
        rlTitle.topMargin = ImmersionBar.getStatusBarHeight(this);
        mVDBinding.title.setLayoutParams(rlTitle);

        mFollowFragment = FollowFragment.newInstance();
        mRecommendFragment = RecommendFragment.newInstance();
//        mNearbyFragment = NearbyFragment.newInstance();

        mVDBinding.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                final TextView tabTv = (TextView) (((LinearLayout) ((LinearLayout) mVDBinding.tab.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));

                Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_home_top_tab_big);
//                Animation animation = new Animation() {
//                };
                animation.setFillAfter(true);
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        tabTv.setTextAppearance(Typeface.BOLD);
                        tabTv.setTextColor(ContextCompat.getColor(getContext(), R.color.bg_black));
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                tabTv.setAnimation(animation);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                final TextView tabTv = (TextView) (((LinearLayout) ((LinearLayout) mVDBinding.tab.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));

                Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_home_top_tab_small);
//                Animation animation = new Animation() {
//                };
                animation.setFillAfter(true);
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        tabTv.setTextAppearance(Typeface.NORMAL);
                        tabTv.setTextColor(ContextCompat.getColor(getContext(), R.color.textColor31));
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                tabTv.setAnimation(animation);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        mVDBinding.tab.addTab(mVDBinding.tab.newTab().setText(R.string.follow), false);
        mVDBinding.tab.addTab(mVDBinding.tab.newTab().setText(R.string.recommend), false);
//        mVDBinding.tab.addTab(mVDBinding.tab.newTab().setText(R.string.nearby), false);

        initViewPager();
    }

    private void initViewPager() {
        mVDBinding.viewPager.setOffscreenPageLimit(2);
        mVDBinding.viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                if (position == 0) {
                    return mFollowFragment;
                } else if (position == 1) {
                    return mRecommendFragment;
                }
//                else if (position == 2) {
//                    return mNearbyFragment;
//                }
                return null;
            }

            @Override
            public int getCount() {
                return 2;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                if (position == 0) {
                    return getString(R.string.follow);
                } else if (position == 1) {
                    return getString(R.string.recommend);
                }
//                else if (position == 2) {
//                    return getString(R.string.nearby);
//                }
                return mVDBinding.tab.getTabAt(position).getText();
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {

            }
        });

        mVDBinding.tab.setupWithViewPager(mVDBinding.viewPager);
        mVDBinding.tab.getTabAt(1).select();
    }
}